package com.bawei.model_sevent.Activity.Fragemnt;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bawei.model_sevent.Activity.Adapter.ShoppingAdapter;
import com.bawei.model_sevent.Activity.MainActivity;
import com.bawei.model_sevent.Activity.MySql.MySql;
import com.bawei.model_sevent.Activity.MySql.MySqlBean;
import com.bawei.model_sevent.R;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class BlankFragment_shoppCar extends Fragment {

    private RecyclerView recyShopping;
    ArrayList<MySqlBean> mySqlBeans;
    private CheckBox all;
    private Button money;
    private TextView showMoney;
    MySqlBean mySqlBean;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_blank_shopp_car, container, false);
        recyShopping = inflate.findViewById(R.id.recy_shopping);
        all = inflate.findViewById(R.id.all);
        money = inflate.findViewById(R.id.money);
//        showMoney = inflate.findViewById(R.id.show_money);
        return inflate;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Inflate the layout for this fragment

        recyShopping.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        mySqlBeans = new ArrayList<>();
        ShoppingAdapter shoppingAdapter = new ShoppingAdapter(mySqlBeans);
        MySql mySql = new MySql(getActivity(), "goods.dp", null, 1);
        SQLiteDatabase writableDatabase = mySql.getWritableDatabase();
        Cursor goods = writableDatabase.query("goods", null, null, null, null, null, null);
        while (goods.moveToNext()) {
            String name = goods.getString(goods.getColumnIndex("name"));
            String images = goods.getString(goods.getColumnIndex("images"));
            mySqlBean = new MySqlBean(name, images);
            mySqlBeans.add(mySqlBean);
            recyShopping.setAdapter(shoppingAdapter);
            shoppingAdapter.notifyDataSetChanged();
        }
        all.setChecked(false);
        all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < mySqlBeans.size(); i++) {
                    mySqlBeans.get(i).checkBox=all.isChecked();
                    shoppingAdapter.notifyDataSetChanged();
                    sum_money();
                }
            }
        });
        shoppingAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
//                shoppingAdapter.remove(mySqlBeans.get(position));
////                writableDatabase.delete("goods.dp","DELETE FROM goods.dp WHERE name=?,images=?",new String[]{mySqlBeans.get(position).getName(),mySqlBeans.get(position).getImages()});
//                shoppingAdapter.notifyDataSetChanged();
                switch (view.getId())
                {
                    case R.id.cb:
                        mySqlBeans.get(position).checkBox=!mySqlBeans.get(position).checkBox;
                        suibian();
                        break;
                    case R.id.add66:
                        mySqlBeans.get(position).content++;
                        shoppingAdapter.notifyDataSetChanged();
                        break;
                    case R.id.del:
                        if (mySqlBeans.get(position).content==1)
                        {
                            Toast.makeText(getActivity(), "不能在减了", Toast.LENGTH_SHORT).show();
                        }else
                        {
                            mySqlBeans.get(position).content--;
                        }
                        shoppingAdapter.notifyDataSetChanged();
                        break;
                    case R.id.img:
                        shoppingAdapter.remove(mySqlBeans.get(position));
                        shoppingAdapter.notifyDataSetChanged();
                        break;
                }
            }
        });
        writableDatabase.close();
        shoppingAdapter.notifyDataSetChanged();
        
        money.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                    if (all.isChecked()==false)
                    {
                        ARouter.getInstance().build("/app/show_shopping").withString("show_name_order", mySqlBean.getName().toString())
                                .withString("show_images_order", mySqlBean.getImages().toString())
                                .navigation();
                    }else
                    {
                        Toast.makeText(getActivity(), "选择的太多了 显示不了", Toast.LENGTH_SHORT).show();
                    }
                }
               
            
        });
    }

    private void sum_money() {
        
    }

    private void suibian() {
        int index=0;
        for (int i = 0; i < mySqlBeans.size(); i++)
        {
            if (!mySqlBeans.get(i).checkBox)
            {
                index=1;
                break;
            }
        }
        if (index==0)
        {
            all.setChecked(true);
        }else
        {
            all.setChecked(false);
        }
    }

}